<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="业务编号" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入业务编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入业务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="user_name">
        <el-input
          v-model="queryParams.user_name"
          placeholder="请输入申请人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核结果" prop="check_status">
        <el-select v-model="queryParams.check_status" placeholder="请选择审核结果" clearable>
          <el-option
            v-for="dict in dict.type.flow_check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker
          v-model="queryParams.apply_time"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['flow:task:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="业务编号" align="center" prop="num" />
      <el-table-column label="业务名称" align="center" prop="title" />
      <el-table-column label="申请人" align="center" prop="user_id" />
      <el-table-column label="申请人姓名" align="center" prop="user_name" />
      <el-table-column label="申请时间" align="center" prop="apply_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.apply_time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请原因" align="center" prop="apply_reason" />
      <el-table-column label="审核结果" align="center" prop="check_status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flow_check_status" :value="scope.row.check_status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['flow:task:edit']"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pager.page"
      :limit.sync="pager.limit"
      @pagination="getList"
    />
    <!-- 添加或修改审批任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-tabs v-model="tab">
        <el-tab-pane label="业务信息" name="base">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" disabled>
        <el-form-item label="业务编号" prop="num">
          <el-input v-model="form.num" placeholder="请输入业务编号" />
        </el-form-item>
        <el-form-item label="业务名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入业务名称" />
        </el-form-item>
        <el-form-item label="申请人" prop="user_name">
          <el-input v-model="form.user_name" placeholder="请输入申请人姓名" />
        </el-form-item>
        <el-form-item label="申请时间" prop="apply_time">
          <el-date-picker clearable
            v-model="form.apply_time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请原因" prop="apply_reason">
          <el-input v-model="form.apply_reason" placeholder="请输入申请原因" />
        </el-form-item>
        <el-form-item label="审核结果" prop="check_status">
          <el-radio-group v-model="form.check_status">
            <el-radio
              v-for="dict in dict.type.flow_check_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
        </el-tab-pane>
        <el-tab-pane label="审核记录" name="step">
          <el-timeline>
            <el-timeline-item
              v-for="(s, index) in stepList"
              :key="index"
              :timestamp="s.ts">
              {{s.content}}
            </el-timeline-item>
          </el-timeline>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { apiGet, apiPost, apiPut, apiDelete, apiDs, apiExport, apiDownload } from "@/api/openapi";

export default {
  name: "FlowTask",
  dicts: ['flow_check_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审批任务表格数据
      taskList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        num: null,
        title: null,
        user_name: null,
        check_status: null,
        apply_time: [],
        "@alias": "t",
        "@exp": {
          num: 'lk',
          title: 'lk',
          user_name: 'lk',
          apply_time: 'bt'
        }
      },
      pager: {
        page: 1,
        limit: 10,
        order: "id desc",
      },
      // 表单参数
      form: {},
      tab: 'base',
      stepList: [],
      // 表单校验
      rules: {
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        target_id: [
          { required: true, message: "业务主键不能为空", trigger: "blur" }
        ],
        num: [
          { required: true, message: "业务编号不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "业务名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询审批任务列表 */
    getList() {
      this.loading = true;
      let params = {};
      params['flow_task'] = {...this.queryParams};
      if(this.queryParams['apply_time'].length == 0){
          params['flow_task']['apply_time'] = null;
      }
      params['@pager'] = this.pager;
      apiGet(params).then(response => {
        this.taskList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        target_id: null,
        num: null,
        title: null,
        user_id: null,
        user_name: null,
        check_status: null,
        apply_time: null,
        apply_reason: null
      };
      this.resetForm("form");
      this.tab = 'base';
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
        let data = {};
        data['flow_task'] = { id:id};
      apiGet(data).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "审批任务";
      });
       apiGet({"@table":"flow_step","@pager":{limit:1000},"task_id":id}).then(response => {
        let stepList = [];
        //第一节点
        let step = {
          id: 0,
          ts: this.parseTime(row.apply_time),
          status: row.check_status,
          content: row.user_name+"发起审批请求"
        }
        stepList.push(step);
        do{
          step = this.filterStep(response.data.rows, step.id);
          if(step == null){
            break;
          }
          
          stepList.push(step);
          //当前节点还在审核或者不通过
          if(step.status == '1' || step.status == '3'){
            break;
          }          
        }while(true);
        this.stepList = stepList;
      });
    },
    filterStep(steps, pid){
      let s = null;
      let users = "";
      for(var i=0;i<steps.length;i++){
        if(steps[i].pid == pid){
          users += ","+steps[i].check_user_name;
          if(steps[i].check_status != '1'){
            s = steps[i];
          }
        }
      }
      if(users.length == 0) {
        return null;
      }else if(s == null){
        return {
          id: 1,
          ts: this.parseTime(s.check_time),
          status: '1',
          content: '等待 '+users.substring(1)+'等进行审批'
        }
      }else if(s.check_status == '2'){
        return {
          id: s.id,
          ts: this.parseTime(s.check_time),
          status: s.check_status,
          content: users.substring(1)+'等接收到审批请求，'+s.check_user_name+'审批通过'
        }
      }else{
        return {
          id: s.id,
          ts: this.parseTime(s.check_time),
          status: s.check_status,
          content: users.substring(1)+'等接收到审批请求，'+s.check_user_name+'审批不通过'
        }
      }      
    },
    /** 导出按钮操作 */
    handleExport() {
        let params = {};
      params['flow_task'] = this.queryParams;
      params['@pager'] = {page:1, limit:10000};
      apiExport(params).then(response => {
        apiDownload(response.msg);
      });
    }
  }
};
</script>
